//
//  SMAccountViewController.m
//  SaveMoney
//
//  Created by user on 2018/11/27.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMAccountViewController.h"

#import "DNK_ImageClippingManager.h"

@interface SMAccountViewController ()<DNKImageClippingDelegate>
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (nonatomic, copy) NSString *imageEncode;
@end

@implementation SMAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    KViewBorderRadius(_button, SBScaleNumber(100)/2, 1, [UIColor grayColor]);
    _imageEncode = [UIImage imageChangeBasr64StringWithImage:_button.currentImage];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_button setBackgroundImage:[UIImage base64ChangeImageWithEncode:SMUserInfoModel.sharedStorage.avatar] forState:UIControlStateNormal];
    _nameTF.text = SMUserInfoModel.sharedStorage.nickName;
}


- (IBAction)touAction:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择照片来源" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[DNK_ImageClippingManager shareManager] getImageFromIpc:UIImagePickerControllerSourceTypePhotoLibrary viewController:self];
        [DNK_ImageClippingManager shareManager].delegate = self;
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[DNK_ImageClippingManager shareManager] getImageFromIpc:UIImagePickerControllerSourceTypeCamera viewController:self];
        [DNK_ImageClippingManager shareManager].delegate = self;
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:action];
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - ImageCropperDelegate
- (void)imageCropperdidFinished:(UIImage *)editedImage {
    
    [_button setBackgroundImage:editedImage forState:UIControlStateNormal];
    
    UIImage *image = [self scaleToSize:editedImage size:CGSizeMake(SBIPHONEWIDTH, SBScaleNumber(200))];
    NSString *encode = [UIImage imageChangeBasr64StringWithImage:image];
    
    _imageEncode = encode;
}

- (IBAction)saveAction:(id)sender {
    if (![_nameTF.text isValid]) {
        [SVProgressHUD showErrorWithStatus:@"昵称为空"];
        return;
    }
    [SVProgressHUD show];
    __weak typeof(self) weakSelf = self;
    [[NetworkAction sharedAction] changeUserInfoWithPhone:[SMUserInfoModel sharedStorage].phone nickname:_nameTF.text avatar:_imageEncode success:^(id model, NSString *message) {
        [SVProgressHUD showSuccessWithStatus:message];
        
        SMUserInfoModel.sharedStorage.avatar = weakSelf.imageEncode;
        SMUserInfoModel.sharedStorage.nickName = weakSelf.nameTF.text;
        [SMUserInfoModel.sharedStorage save];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            
        });
    } failuer:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
    
    
}

- (UIImage *)scaleToSize:(UIImage *)image size:(CGSize)size {
    //创建一个bitmap的context
    //并把他设置成当前的context
    UIGraphicsBeginImageContext(size);
    //绘制图片的大小
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //从当前context中创建一个改变大小后的图片
    UIImage *endImage=UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return endImage;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
